package com.blog.Bard.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.Bard.base.pojo.BardArticle;
import com.blog.Bard.base.pojo.Bardarticleandlabel;
import com.blog.Bard.base.pojo.User;
import com.blog.Bard.base.service.BardArticleService;
import com.blog.Bard.base.service.BardArticleTypeService;
import com.blog.Bard.common.controller.BaseController;
import com.blog.Bard.common.util.Pager;

/**
 * 文章
 * @author phil
 *
 */
@Controller
@RequestMapping("/base/article")
public class BardArticleController extends BaseController {

	@Autowired
	private BardArticleService bardArticleService;
	
	@Autowired
	private BardArticleTypeService bardArticleTypeService;
	
	/**
	 * 列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model model) {
		logger.info("execute the BardArticleController 's list method...");
		return "article/list";
	}
	
	/**
	 * 文章分类分页
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/pageList")
	public Map<String,Object> pageList(HttpServletRequest req, HttpServletResponse res) {
		logger.info("execute the BardArticleController 's pageList method...");
		Map<String,Object> result = new HashMap<String, Object>();
		Map<String,Object> params = new HashMap<String, Object>();
		Pager data = null;
		try {
			Integer pageNo = super.getInt(req, "page", 1);
			Integer pagesize = super.getInt(req, "limit", 10);
			Integer type = super.getInt(req, "type");
			
			params.put("pageNo", pageNo);
			params.put("pagesize", pagesize);
			params.put("type", type);
			
			data = bardArticleService.pageList(params);
			
			result = super.getSuccessResultMap("ok", data.getData(), data.getTotalRecord());
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
	/**
	 * 	编辑页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest req, HttpServletResponse res, Model model) {
		logger.info("execute the BardArticleController 's addPage method...");
		BardArticle t = new BardArticle();
		try {
			Integer id = super.getInt(req, "id", 0);
			if(id!=0) {
				t = bardArticleService.getOne(id);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("entity", t);
		return "article/addPage";
	}
	
	/**
	 * 	保存
	 * @param req
	 * @param res
	 * @param t
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Map<String,Object> save(HttpServletRequest req, HttpServletResponse res, BardArticle t) {
		logger.info("execute the BardArticleController 's save method...");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			User u = super.getUser(req, res);
			if(u!=null) {
				bardArticleService.save(t, u);
				result = super.getSuccessResultMap("ok", null);
			}else {
				result = super.getErrorResultMap("当前用户为空！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
	/**
	 * 	删除
	 * @param req
	 * @param res
	 * @param t
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Map<String,Object> del(HttpServletRequest req, HttpServletResponse res) {
		logger.info("execute the BardArticleController 's del method...");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Integer id = super.getInt(req, "id", 0);
			if(id==0) {
				result = super.getErrorResultMap("fail");
			}else {
				bardArticleService.del(id);
				result = super.getSuccessResultMap("ok", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
	/**
	 * 	设置文章类型
	 * @param req
	 * @param res
	 * @param model
	 * @return
	 */
	@RequestMapping("/addType")
	public String addType(HttpServletRequest req, HttpServletResponse res, Model model) {
		logger.info("execute the BardArticleController 's addType method...");
		Integer id = 0;
		try {
			id = super.getInt(req, "id", 0);
		}catch(Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("id", id);
		return "article/addType";
	}
	
	/**
	 * 	查找所有文章类型
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getAllType")
	public Map<String,Object> getAllType(HttpServletRequest req, HttpServletResponse res) {
		logger.info("execute the BardArticleController 's getAllType method...");
		Map<String,Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> typeList = new ArrayList<Map<String, Object>>();
		try {
			//所有类型
			typeList = bardArticleTypeService.getAllType();
			result = super.getSuccessResultMap("ok", typeList);
			
			//这里主要用于文章设置类型页面时，使用
			int id = super.getInt(req, "id");
			if(id!=0) {
				Map<String, Object> articleTypeInfo = bardArticleService.getArticleType(id);
				if(articleTypeInfo!=null) {
					result.put("articleTypeInfo", articleTypeInfo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
	/**
	 * 	保存文章类型
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveType")
	public Map<String,Object> saveType(HttpServletRequest req, HttpServletResponse res) {
		logger.info("execute the BardArticleController 's saveType method...");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Integer id = super.getInt(req, "id", 0);
			Integer articleType = super.getInt(req, "articleType", 0);
			if(id!=0 && articleType!=0) {
				bardArticleService.saveType(id, articleType);
				result = super.getSuccessResultMap("ok", null);
			}else {
				result = super.getErrorResultMap("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
	/**
	 * 设置标签
	 * @param req
	 * @param res
	 * @param model
	 * @return
	 */
	@RequestMapping("/addLabel")
	public String addLabel(HttpServletRequest req, HttpServletResponse res, Model model) {
		logger.info("execute the BardArticleController 's addLabel method...");
		Integer id = 0;
		try {
			id = super.getInt(req, "id");
			Map<String, Object> labelsMap = bardArticleService.getArticleLabels(id);
			if(labelsMap!=null) {
				model.addAttribute("labelIds", labelsMap.get("labelIds"));
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("id", id);
		return "article/addLabel";
	}
	
	/**
	 * 保存标签
	 * @param req
	 * @param res
	 * @param arry
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveLabels")
	public Map<String,Object> saveLabels(HttpServletRequest req, HttpServletResponse res, 
			@RequestBody Bardarticleandlabel[] arr) {
		logger.info("execute the BardArticleController 's saveLabels method...");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			if(arr!=null && arr.length>0) {
				bardArticleService.saveBardarticleandlabel(arr);
				result = super.getSuccessResultMap("ok", null);
			}else {
				result = super.getErrorResultMap("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = super.getErrorResultMap("fail");
		}
		return result;
	}
	
}
